package com.gxt.controller;



import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.gxt.pojo.Users;
import com.gxt.pojo.VO.UsersVO;
import com.gxt.service.UserService;
import com.gxt.utils.IMoocJSONResult;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

@RestController
@Api(value = "用户相关业务的接口", tags = { "用户相关业务的controller" })
@RequestMapping("/user")
public class UserController extends BasicController {
	@Autowired
	private UserService userservice;
	
	@ApiOperation(value = "用户上传头像", notes = "用户上传头像的接口")
	@ApiImplicitParam(name = "userId",value = "用户Id",required = true,
						dataType = "String",paramType = "query")
	@PostMapping("/uploadFace")
	public IMoocJSONResult uploadFace(String userId,@RequestParam("file") MultipartFile[] files) throws Exception {
		if(StringUtils.isBlank(userId)) {
			return IMoocJSONResult.errorMsg("用户Id不能为空！");
		}
//		 文档保存命名空间
		String fileSpace = "E:/gxt_videos";
//		 保存到数据库的相对路径
		String uploadPathDB = "/" + userId + "/face";
		
		FileOutputStream fileOutputStream = null;
		InputStream inputStream = null;
		
		try {
			if(files != null && files.length > 0) {
			
				
				String fileName = files[0].getOriginalFilename();
				
				if(StringUtils.isNotBlank(fileName)) {
//				文件上传的最终保存路径
					String finalFacePath = fileSpace + uploadPathDB + "/" + fileName;
//				设置数据库保存的路径
					uploadPathDB += ("/" + fileName);
					
//				作判断  如果父文件夹不存在则创建
					File outFile = new File(finalFacePath);
					if(outFile.getParentFile() != null || !outFile.getParentFile().isDirectory()) {
//					创建父文件夹
						outFile.getParentFile().mkdirs();
					}
					
					fileOutputStream = new FileOutputStream(outFile);
					inputStream = files[0].getInputStream();
					
					IOUtils.copy(inputStream, fileOutputStream);
//				考虑关闭输出流的情况
				}
			}else {
				return IMoocJSONResult.errorMsg("上传出错！");
			}
		}
		catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return IMoocJSONResult.errorMsg("上传出错...");
		} finally {
			if(fileOutputStream!=null) {
				fileOutputStream.flush();
				fileOutputStream.close();
			}
		}
//		上传成功  调用UserService存入数据库
		Users user = new Users();
		user.setId(userId);
		user.setFaceImage(uploadPathDB); //数据库保存的路径
//		!记得调用接口
		userservice.updateUserInfo(user);
		
		return IMoocJSONResult.ok(uploadPathDB);
	}
	
	@ApiOperation(value = "查询用户信息", notes = "查询用户信息的接口")
	@ApiImplicitParam(name = "userId",value = "用户Id",required = true,
						dataType = "String",paramType = "query")
	@PostMapping("/query")
	public IMoocJSONResult query(String userId) throws Exception {
		if(StringUtils.isBlank(userId)) {
			return IMoocJSONResult.errorMsg("userId不能为空！");
		}
		Users userInfo = userservice.queryUserInfo(userId);
		
		UsersVO userVO = new UsersVO();
		BeanUtils.copyProperties(userInfo, userVO);
		
		return IMoocJSONResult.ok(userVO);
	}

	
}
